﻿<%@ WebHandler Language="C#" Class="IssuedAdd" %>

using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.SessionState;

public class IssuedAdd : IHttpHandler, IRequiresSessionState
{

    private HttpContext context;
    public void ProcessRequest(HttpContext context)
    {
        this.context = context;
        context.Response.ContentType = "text/plain";

        /* 外部数据绑定 */
        string Item_User_Name = context.Request.Params["Item_User_Name"].ToString().Trim();
        string Item_User_Id = context.Request.Params["Item_User_Id"].ToString().Trim();
        string Item_User_Email = context.Request.Params["Item_User_Email"].ToString().Trim();
        string Item_User_Tel = context.Request.Params["Item_User_Tel"].ToString().Trim();
        string Item_Text = context.Request.Params["Item_Text"].ToString().Trim();
        
        string Item_Title = context.Request.Params["Item_Title"].ToString().Trim();
        int Item_Column = Convert.ToInt32(context.Request.Params["Item_Column"].ToString().Trim());
        int Item_Lang = Convert.ToInt32(context.Request.Params["item_lang"].ToString().Trim());
        string VerificationImgCode = context.Request.Params["VerificationImgCode"].ToString().Trim();
        string Item_Summary = context.Request.Params["Item_Summary"].ToString().Trim();
        
        /* 用户本地信息 */
        string User_IP = context.Request.UserHostAddress;
        string User_DNS = context.Request.UserHostName;

        /* 验证码安全性验证 */
        if (context.Session["VerificationImgCode"] == null)
            End("-3");
        else if (VerificationImgCode.ToLower() != context.Session["VerificationImgCode"].ToString().ToLower())
            End("-3");

        Verification verification = new Verification();

        Regex chr;
        if(!verification.VerificationMinMaxLength(Item_User_Name,2,80))
            End("-1");
        chr = new Regex(verification.Email);
        if (!chr.IsMatch(Item_User_Email))
            End("-1");
        chr = new Regex(verification.ValidateUserId);
        if (!chr.IsMatch(Item_User_Id))
            End("-1");
        chr = new Regex(verification.ValidatePhone);
        if (!chr.IsMatch(Item_User_Tel))
            End("-1");

        /* 信息HTML ENCODE */
        Item_User_Name = Utils.HtmlEncode(Item_User_Name);
        Item_Title = Utils.HtmlEncode(Item_Title);
        Item_Text = Utils.HtmlEncode(Item_Text);
        Item_Summary = Utils.HtmlEncode(Item_Summary);
        
        /* 添加到数据库 */
        Issued issued=new Issued();
        int Status = issued.Add(Item_User_Name, Item_User_Id, Item_User_Email, Item_User_Tel, Item_Column, Item_Text, Item_Title, User_IP, User_DNS, Item_Summary, Item_Lang);
        /* 验证码缓存清理 */
        context.Session["VerificationImgCode"] = null;
        End(Status.ToString());
    }

    public void End(string text)
    {
        context.Response.Write(text);
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}